<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');


/**
 * Initialize Messages that will be shown on the company screen.
 * @access public 
 */
function init_company()
{
    $ci = &get_instance();
    
    // initialize company messages
    init_messages('Company');
    
//    // initialize table query table columns
//    $ci->alias = 'id, name, description, date_created, date_modified, status';
//    
//    // initialize more columns to be shown
//    $ci->more_columns = 'id, name, description, date_created, date_modified, status';
//    
//    // initialize search columns
//    $ci->search_columns = array('name', 'description', 'status');
//    
//    // initialize table headers
//    $table_headers = array();
//    $table_headers['name'] = 'Name';
//    $table_headers['description'] = 'Description';
//    $table_headers['date_created'] = 'Date Created';
//    $table_headers['date_modified'] = 'Date Modified';
//    $table_headers['status'] = 'Status';
//    $ci->table_headers = create_datatable_headers($table_headers, 'companies', 2);
//
//    // initialize customized table columns
//    $ci->custom_columns = array();
//    
//    $date_created = array();
//    $date_created['keys'] = 'cms_date(date_created)';
//    $date_created['markup'] = '$1';
//    $ci->custom_columns['date_created'] = $date_created;
//    
//    $date_modified = array();
//    $date_modified['keys'] = 'cms_date(date_modified)';
//    $date_modified['markup'] = '$1';
//    $ci->custom_columns['date_modified'] = $date_modified;
//    
//    $status['keys'] = 'cms_status_toggle(status, companies/confirm_update/, id)';
//    $status['markup'] = '$1';
//    $ci->custom_columns['status'] = $status;
}

/*
 * Method for validating form inputs from the company.
 * @access public
 * @retrun bool Returns true if the validation was successful, otherwise redirects to the company form.
 */
function valid_company($action)
{
    $ci = &get_instance();

    $ci->form_validation
            ->set_rules('name', 'name', 'trim|required|max_length[20]|xss_clean')
            ->set_rules('description', 'description', 'trim|required|max_length[1000]|xss_clean');
//          ->set_rules('status', 'status', 'trim|required|max_length[10]|xss_clean');
    
    if($action == 'edit')
    {
        $ci->form_validation->set_rules('id', 'ID', 'trim|required|numeric|max_length[10]|xss_clean');
    }
    
    if ($ci->form_validation->run() == FALSE)
    {
        if($action == 'add')
        {
            view_company_form('Add Company', 'add_failed');
        } else
        {
            $company = $ci->company_model->get_by_id( set_value('id') );
            view_company_form('Edit Company', 'edit_failed', $company);
        }
    } else
    {
        return TRUE;
    }
}

/**
 * Displays companies list page.
 * @access public
 * @param array $content An array of companies to be shown.
 * @param string $message The type of message to be shown.
 */
function view_companies_list($content = NULL, $message_type = NULL)
{
    $ci = &get_instance();

    // create breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Companies');
    
    $data = array();
    
    // set message
    $message                    = get_message($message_type);
    $data['message_type']       = $message['message_type'];
    $data['message']            = $message['message'];
    $data['message_title']      = $message['title'];
    $data['message_class']      = $message['class'];
    
    if($content === NULL)
    {
        $data['companies'] = $ci->company_model->get_all();
    } else
    {
        $data['companies'] = $content;
    }

    // active navigation
    $data['navigation'] = 'companies';

    // build breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Companies');

    // build template
    $ci->template
            ->title('Companies')
            ->set_layout('main')
            ->set_partial('navigation', 'layouts/navigation')
            ->set_partial('footer', 'layouts/footer')
            ->set_partial('notifications', 'layouts/notifications')
//            ->set_partial('modal', 'layouts/modal')
            ->build('company_list', $data);
}

/**
 * Displays company info.
 * @access public
 * @param array $content An array of record info.
 */
function view_company_preview($content = NULL)
{
    $ci = &get_instance();

    // create breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Preview Company');
    
    $data = array();

    if($content === NULL)
    {
        $data['companies'] = $ci->company_model->get_all();
    } else
    {
        $data['companies'] = $content;
    }

    // active navigation
    $data['navigation'] = 'companies';

    // build breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Companies', base_url() . 'companies');
    $ci->breadcrumb->add_crumb('Company Preview');
    
    // build template
    $ci->template
            ->title('Company')
            ->set_layout('main')
            ->set_partial('navigation', 'layouts/navigation')
            ->set_partial('footer', 'layouts/footer')
//            ->set_partial('notifications', 'layouts/notifications')
//            ->set_partial('modal', 'layouts/modal')
            ->build('company_preview', $data);
}

/**
 * Displays the form for adding or editing companies.
 * @access public
 * @param string $title String representing the title page.
 * @param string $crumb The string that will be appended to the end of the breadcrumb.
 * @param string $message The type of message that will be shown on the form. Default value is null.
 * @param array $record An array containing record info to be edited. Default value is null.
 */
function view_company_form($title, $message_type = NULL, $record = NULL)
{
    $ci = &get_instance();
    $data = array();
    
    // set message
    $message                    = get_message($message_type);
    $data['message_type']       = $message['message_type'];
    $data['message']            = $message['message'];
    $data['message_title']      = $message['title'];
    $data['message_class']      = $message['class'];

    // set form data
    $data['action'] = $title == 'Add Company' ? 'add' : 'edit';
    $data['add'] = $title == 'Add Company' ? TRUE : FALSE;
    $data['add_another'] = $ci->input->post('add-another') ? ' checked="checked"' : '';

    // set reservation data
    $data['id'] = isset($record['id']) ? $record['id'] : set_value('id');
    
    $data['name'] = isset($record['name']) ? $record['name'] : set_value('name');
    $data['name-status'] = form_error('name') ? ' has-error' : '';
    
    $data['description'] = isset($record['description']) ? $record['description'] : set_value('description');
    $data['description-status'] = form_error('description') ? ' has-error' : '';
    
    $data['status'] = isset($record['status']) ? $record['status'] : set_value('status');
    
    // set active navigation
    $data['navigation'] = 'companies';
    
    // create breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Companies', base_url() . 'companies');
    $ci->breadcrumb->add_crumb($title);

    // build template
    $ci->template
            ->title($title)
            ->set_layout('main')
            ->set_partial('navigation', 'layouts/navigation')
            ->set_partial('footer', 'layouts/footer')
            ->set_partial('notifications', 'layouts/notifications')
//            ->set_partial('modal', 'layouts/modal')
            ->build('company_form', $data);
    
}


/**
 * Formats an array of companies to valid response.
 * @access public
 * @param array $countries Array of companies
 */
function response_companies($companies)
{
    $ci = &get_instance();
    
    $response_companies = array();
    foreach($companies as $company)
    {
        $response_company                       = array();
        $response_company['id']                 = clean( $company['id'] );
        $response_company['name']               = clean( $company['name'] );
        $response_company['description']        = clean( $company['description'] );
        $response_company['address']            = clean( $company['address'] );  
        
        array_push($response_companies, $response_company);
    } 
    
    // we format our response
    if( count($response_companies) > 0 ) // check if not empty
    {
        format_response('success', 200, 'Response Companies.', NULL, $response_companies);
    }
    else
    {
        format_response('success', 200, 'No Data Available', NULL);
    }
}


/* End of file company_helper.php */
/* Location: ./application/companies/cms-companies/helpers/company_helper.php */